//
//  GXDailyNormalView.m
//  GxLanOuProject
//
//  Created by 甘霞 on 15/10/27.
//  Copyright © 2015年 甘霞. All rights reserved.
//

#import "GXDailyNormalView.h"
#import "GXDailyModel.h"
#import "NSString+GXSizeWithText.h"
#import "UIImageView+WebCache.h"
#import "UIImage+GIF.h"

//日期顶部
#define kDailyDateLabelTopBorder 40
//名字顶部
#define kDailyNameLabelTopBorder 30
//标题顶部
#define  kDailyTitleLabelTopBorder 10
//内容顶部
#define kDailyContentLabelTopBorder 30
//黄色图标顶部
#define kDailyYellowImageTopBorder 40
//下拉顶部
#define kDailyDropLabelTopBorder 20

//姓名高度
#define kNameLabelHeight 30
//下拉高度
#define kdropLabelHeight 30
//日期高度
#define kDateLabelHeihgt 50
//黄色图标高度
#define kYellowImageViewSize 40
//边界
#define kDailySideBorder 40
//标题高度
#define kTitleLabelHeight 40

//内容字号
#define kDailyContentFontSize 18
//内容行间距
#define LINESPACE 20
//日期字号
#define kDateFontSize 30
//标题字号
//#define kTitleFontSize 30

@interface GXDailyNormalView ()

@property (nonatomic, retain) UILabel *dateLabel;
@property (nonatomic, retain) UILabel *nameLabel;
@property (nonatomic, retain) UILabel *titleLable;
@property (nonatomic, retain) UILabel *introLabel;
@property (nonatomic, retain) UIImageView *yellowImageView;
@property (nonatomic, retain) UILabel *introduceLabel;
@property (nonatomic, retain) UIImageView *bgImageView;
@property (nonatomic, retain) UIButton *backButton;

@end

@implementation GXDailyNormalView

- (instancetype)initWithFrame:(CGRect)frame dailyModel:(GXDailyModel *)dailyModel
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews:dailyModel];
    }
    return self;
}

- (void)createSubviews:(GXDailyModel *)dailyModel {
    //手势
    UISwipeGestureRecognizer *swipe= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    swipe.direction = UISwipeGestureRecognizerDirectionDown;
    [self addGestureRecognizer:swipe];
    
    //背景
    self.bgImageView = [[UIImageView alloc] initWithFrame:self.frame];
    [self.bgImageView sd_setImageWithURL:dailyModel.bg_img_url placeholderImage:[UIImage sd_animatedGIFNamed:@"loading"]];
    [self addSubview:self.bgImageView];
    
    //文本宽度
    CGFloat labelWidth = self.frame.size.width - 2 *  kDailySideBorder;
    
    //日期
    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(kDailySideBorder, kDailyDateLabelTopBorder, labelWidth, kDateLabelHeihgt)];
    [self.dateLabel setTextAlignment:NSTextAlignmentCenter];
    [self addSubview:self.dateLabel];
    [self.dateLabel setFont:[UIFont systemFontOfSize: kDateFontSize]];
    [self.dateLabel.layer setBorderWidth:3];
    [self.dateLabel.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    [self.dateLabel setTextColor:[UIColor whiteColor]];
    NSString *dateString = [dailyModel.release_date stringByReplacingOccurrencesOfString:@"-" withString:@"." ];
    self.dateLabel.text = [NSString stringWithFormat:@"下午茶 %@", dateString];
    
    //姓名
    CGFloat nameLabelY = CGRectGetMaxY(self.dateLabel.frame) + kDailyNameLabelTopBorder;
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(kDailySideBorder, nameLabelY, labelWidth, kNameLabelHeight)];
    [self.nameLabel setTextColor:[UIColor whiteColor]];
    self.nameLabel.text = dailyModel.sub_title;
    [self.nameLabel setTextAlignment:NSTextAlignmentCenter];
    [self addSubview:self.nameLabel];
    
    //标题
    CGFloat titleLabelY = CGRectGetMaxY(self.nameLabel.frame) + kDailyTitleLabelTopBorder;
    self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(kDailySideBorder, titleLabelY, labelWidth, kTitleLabelHeight)];
    [self.titleLable setTextColor:[UIColor whiteColor]];
    [self.titleLable setFont:[UIFont systemFontOfSize:kTitleFontSize]];
    [self.titleLable setTextAlignment:NSTextAlignmentCenter];
    [self addSubview:self.titleLable];
    self.titleLable.text = dailyModel.title;
    
    //内容
    CGFloat introLabelY = CGRectGetMaxY(self.titleLable.frame) + kDailyContentLabelTopBorder;
    self.introLabel = [[UILabel alloc] init];
    self.introLabel.text = dailyModel.intro;
    [self.introLabel setTextColor:[UIColor whiteColor]];
    CGRect introLabelSize = [self.introLabel.text SizeWithTextFontSize:kDailyContentFontSize textWidth:labelWidth];
    [self.introLabel setTextAlignment:NSTextAlignmentCenter];
    [self.introLabel setFrame:CGRectMake(kDailySideBorder, introLabelY, labelWidth, introLabelSize.size.height)];
    [self addSubview:self.introLabel];
    [self.introLabel setNumberOfLines:7];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.introLabel.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:LINESPACE];//调整行间距
    [self.introLabel setTextAlignment:NSTextAlignmentCenter];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.introLabel.text length])];
    self.introLabel.attributedText = attributedString;
    [self.introLabel sizeToFit];
    
    //黄色图标
    CGFloat yellowImageViewY = CGRectGetMaxY(self.introLabel.frame) + kDailyYellowImageTopBorder;
    UIImage *yellowImage = [UIImage imageNamed:@"pull-to-start"];
    self.yellowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 - kYellowImageViewSize / 2, yellowImageViewY, yellowImage.size.width, yellowImage.size.height)];
    self.yellowImageView.image = yellowImage;
    [self addSubview:self.yellowImageView];
    
    //说明
    self.introduceLabel = [[UILabel alloc] initWithFrame:CGRectMake(kDailySideBorder, self.frame.size.height - 2 * kDailyDropLabelTopBorder, labelWidth, kDailyDropLabelTopBorder)];
    [self.introduceLabel setTextColor:[UIColor whiteColor]];
    [self.introduceLabel setTextAlignment:NSTextAlignmentCenter];
    [self addSubview:self.introduceLabel];
    self.introduceLabel.text = [NSString stringWithFormat:@"共%lu篇内容,%@分钟",(unsigned long)dailyModel.contents.count, dailyModel.read_time];
    
    //历史记录
    self.historyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.historyBtn setFrame:CGRectMake(self.frame.size.width - 60, self.frame.size.height - 3.5/2 * 50, 50, 50)];
    [self.historyBtn setImage:[UIImage imageNamed:@"icon-afternoon-tea-entry"] forState:UIControlStateNormal];
    [self.historyBtn addTarget:self
                        action:@selector(historyBtnAction)
              forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.historyBtn];
    
    //后退按钮
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backButton setFrame:CGRectMake(10, self.frame.size.height - 3.5/2 * 50, 50, 50)];
    [self.backButton setImage:[UIImage imageNamed:@"btn_back"] forState:UIControlStateNormal];
    [self.backButton addTarget:self
                        action:@selector(backBtnAction)
              forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.backButton];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches
           withEvent:(UIEvent *)event {
    
    self.pointArr = [NSMutableArray array];
    UITouch *touch = [touches anyObject];
    CGPoint startPoint = [touch locationInView:touch.view];
    [self.pointArr addObject:[NSValue valueWithCGPoint:startPoint]];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches
           withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint movePoint = [touch locationInView:touch.view];
    [self.pointArr addObject:[NSValue valueWithCGPoint:movePoint]];
    for (id view in [self subviews]) {
        if (view != self.bgImageView) {
//            [view setAlpha:0.5];
        }
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches
           withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint endPoint = [touch locationInView:touch.view];
    [self.pointArr addObject:[NSValue valueWithCGPoint:endPoint]];
}

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe {
    
    [self.delegate gestureRecongnizerAction];
}

- (void)historyBtnAction {
    [self.delegate dailyHistoryClickProtocol];
}

- (void)backBtnAction {
    [self.delegate dailuBackClickProtocol];
}


@end
